﻿#region Using directives

using System;
using System.Collections.Generic;
using System.IO;

#endregion

namespace BesAsm.MasterData.Entities
{	
	///<summary>
	/// An object representation of the 'LAYER_FILES' table. [No description found the database]	
	///</summary>
	/// <remarks>
	/// This file is generated once and will never be overwritten.
	/// </remarks>	
	[Serializable]
	[CLSCompliant(true)]
	public partial class LayerFile : LayerFileBase
	{
    public static string[] LAYERS_MASTER = new string[] { "Links", "Nodes", "Dscs" };
    public static string[] LAYERS_RESULTS = new string[] { "LINK_RESULTS", "NODE_RESULTS", "DSC_RESULTS" };

    public static class Categories
    {
      public const string MasterData = "Master Data";
      public const string ModelResults = "Model Results";
    }

    public static Dictionary<string, string[]> LayerTypes = GetLayerTypes();
    
    private static Dictionary<string, string[]> GetLayerTypes()
    {
      Dictionary<string, string[]> _layerTypes = new Dictionary<string, string[]>();
      _layerTypes.Add(Categories.MasterData, LAYERS_MASTER);
      _layerTypes.Add(Categories.ModelResults, LAYERS_RESULTS);

      return _layerTypes;
    }
            
		#region Constructors

		///<summary>
		/// Creates a new <see cref="LayerFile"/> instance.
		///</summary>
		public LayerFile():base(){}	
		
		#endregion

    public void WriteLayer(string path)
    {
      string file = Path.Combine(path, this.Name + ".lyr");
      BinaryWriter writer = null;
      try
      {
        FileStream stream = new FileStream(file, FileMode.Create);
        writer = new BinaryWriter(stream);
        writer.Write(this.LayerFile);
      }
      finally
      {
        if (writer != null)
          writer.Close();
      }
    }

    public void ReadLayer(string file)
    {
      byte[] bytes;
      BinaryReader reader = null;
      try
      {
        FileStream stream = new FileStream(file, FileMode.Open);
        reader = new BinaryReader(stream);

        bytes = reader.ReadBytes((int)stream.Length);

        this.LayerFile = bytes;
      }
      finally
      {
        if (reader != null)
          reader.Close();
      }      
    }

	}

}
